package com.ldj.common;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ldj.hibernate.DataDictionary;
import com.ldj.hibernate.DataDictionaryDAO;
import com.ldj.hibernate.FunctionMenu;
import com.ldj.hibernate.FunctionMenuDAO;
import com.ldj.hibernate.SuperviseOrgan;
import com.ldj.hibernate.SuperviseOrganDAO;

public class MenuBuilder {
	
	
	 // 监察机构树
	 public String getSuperviseOrganTree(HttpServletRequest request, HttpServletResponse response, String rootName, String rootUrl, String url, String value)
	    {
		 	SuperviseOrganDAO dao = new SuperviseOrganDAO(request, response);
//		 	Catagory topCata = dao.getTopCatalog(code);
		 	StringBuffer strBuf = new StringBuffer("");
		 	if(value == null)
		 		strBuf.append("d = new dTree('d');\n");
		 	else
		 		strBuf.append("d = new dTree('d', '" + value + "');\n");
	 		strBuf.append("d.add(0,-1,'" + rootName + "', '" + rootUrl + "', '', 'rightMenu');\n");
			List list = dao.getSonCatalog("");
			if(list != null && list.size() > 0){
				for(int i = 0; i < list.size(); i ++){
					SuperviseOrgan sup = (SuperviseOrgan)list.get(i);
					String pcode = "0";
		 			String code = sup.getSupOrganCode();
		 			if(code.length() > 3)
		 				pcode = code.substring(0, code.length() - 3);
		 			strBuf.append("d.add('" + code + "','" + pcode + "','" + sup.getSupOrganName() + "','" + url.replace("$code",sup.getSupOrganCode()) + "', '', 'rightMenu');\n");
//					strBuf.append("d.add('" + sup.getSupOrganCode() + "',0,'" + sup.getSupOrganName() + "','" + url.replace("$code",sup.getSupOrganCode()) + "', '', 'rightMenu');\n");
//					strBuf.append(this.getSonSunperviseOrganTree(request, response, sup.getSupOrganCode(), url));
				}
			}
			strBuf.append("document.write(d);\n");
	        return strBuf.toString();
	    }
	 
//	 public String getSonSunperviseOrganTree(HttpServletRequest request, HttpServletResponse response, String code, String url){
//		 SuperviseOrganDAO dao = new SuperviseOrganDAO(request, response);
//		 List list = dao.getSonCatalog(code);
//		 StringBuffer strBuf = new StringBuffer("");
//		 if(list != null && list.size() > 0){
//		 	for(int i = 0; i < list.size(); i ++){
//		 		SuperviseOrgan sup = (SuperviseOrgan)list.get(i);
//		 		strBuf.append("d.add('" + sup.getSupOrganCode() + "','" + code + "','" + sup.getSupOrganName() + "','" + url.replace("$code",sup.getSupOrganCode()) + "', '', 'rightMenu');\n");
//		 		strBuf.append(this.getSonSunperviseOrganTree(request, response, sup.getSupOrganCode(), url));
//		 	}
//		 }
//		 return strBuf.toString();
//	 }
	 
	 // 地区树
	 public String getAreaTree(HttpServletRequest request, HttpServletResponse response, String rootName, String rootUrl, String url, String value)
	    {	        
	        DataDictionaryDAO dao = new DataDictionaryDAO(request, response);
		 	StringBuffer strBuf = new StringBuffer("");
		 	if(value == null)
		 		strBuf.append("d = new dTree('d');\n");
		 	else
		 		strBuf.append("d = new dTree('d', '" + value + "');\n");
		 		strBuf.append("d.add(0,-1,'" + rootName + "', '" + rootUrl + "', '', 'rightMenu');\n");
			 	List list = dao.getSonCatalog("002");
			 	if(list != null && list.size() > 0){
			 		for(int i = 0; i < list.size(); i ++){
			 			DataDictionary data = (DataDictionary)list.get(i);
			 			String pcode = "0";
			 			String code = data.getDataDictionaryCode();
			 			if(code.length() > 6)
			 				pcode = code.substring(0, code.length() - 3);
			 			strBuf.append("d.add('" + code + "','" + pcode + "','" + data.getDataDictionaryName() + "','" + url.replace("$code",data.getDataDictionaryCode()) + "', '', 'rightMenu');\n");
//			 			strBuf.append("d.add('" + data.getDataDictionaryCode() + "',0,'" + data.getDataDictionaryName() + "','" + url.replace("$code",data.getDataDictionaryCode()) + "', '', 'rightMenu');\n");
//			 			strBuf.append(this.getSonDataDictionaryTree(request, response, data.getDataDictionaryCode(), url));
			 		}
			 	}
			 	strBuf.append("document.write(d);\n");
	        return strBuf.toString();
	    }
	 
	 // 数据字典树
	 public String getDataDictionaryTree(HttpServletRequest request, HttpServletResponse response, String rootName, String rootUrl, String url)
	    {
		 	DataDictionaryDAO dao = new DataDictionaryDAO(request, response);
		 	StringBuffer strBuf = new StringBuffer("");
		 		strBuf.append("d = new dTree('d');\n");
		 		strBuf.append("d.add(0,-1,'" + rootName + "', '" + rootUrl + "', '', 'rightMenu');\n");
			 	List list = dao.getSonCatalog("");
			 	if(list != null && list.size() > 0){
			 		for(int i = 0; i < list.size(); i ++){
			 			DataDictionary data = (DataDictionary)list.get(i);
			 			String pcode = "0";
			 			String code = data.getDataDictionaryCode();
			 			if(code.length() > 3)
			 				pcode = code.substring(0, code.length() - 3);
			 			strBuf.append("d.add('" + code + "','" + pcode + "','" + data.getDataDictionaryName() + "','" + url.replace("$code",data.getDataDictionaryCode()) + "', '', 'rightMenu');\n");
//			 			strBuf.append("d.add('" + data.getDataDictionaryCode() + "',0,'" + data.getDataDictionaryName() + "','" + url.replace("$code",data.getDataDictionaryCode()) + "', '', 'rightMenu');\n");
//			 			strBuf.append(this.getSonDataDictionaryTree(request, response, data.getDataDictionaryCode(), url));
			 		}
			 	}
			 	strBuf.append("document.write(d);\n");
	        return strBuf.toString();
	    }
	 
//	 public String  getSonDataDictionaryTree(HttpServletRequest request, HttpServletResponse response, String code, String url){
//		 DataDictionaryDAO dao = new DataDictionaryDAO(request, response);
//		 List list = dao.getSonCatalog(code);
//		 StringBuffer strBuf = new StringBuffer("");
//		 if(list != null && list.size() > 0){
//		 	for(int i = 0; i < list.size(); i ++){
//		 		DataDictionary data = (DataDictionary)list.get(i);
//		 		strBuf.append("d.add('" + data.getDataDictionaryCode() + "','" + code + "','" + data.getDataDictionaryName() + "','" + url.replace("$code",data.getDataDictionaryCode()) + "', '', 'rightMenu');\n");
//	 			strBuf.append(this.getSonDataDictionaryTree(request, response, data.getDataDictionaryCode(), url));
//		 	}
//		 }
//		 return strBuf.toString();
//	 }
	 
	 // 功能菜单树
	 
	 public String getFunctionMenuTree(HttpServletRequest request, HttpServletResponse response, String rootName, String rootUrl, String url, String value)
	    {
		 	FunctionMenuDAO dao = new FunctionMenuDAO(request, response);
		 	StringBuffer strBuf = new StringBuffer("");
		 	if(value == null)
		 		strBuf.append("d = new dTree('d');\n");
		 	else
		 		strBuf.append("d = new dTree('d', '" + value + "');\n");
	 		strBuf.append("d.add(0,-1,'" + rootName + "', '" + rootUrl + "', '', 'rightMenu');\n");
			List list = dao.getSonCatalog("");
			if(list != null && list.size() > 0){
				for(int i = 0; i < list.size(); i ++){
					FunctionMenu fm = (FunctionMenu)list.get(i);
					String pcode = "0";
		 			String code = fm.getFunctionMenuCode();
		 			if(code.length() > 3)
		 				pcode = code.substring(0, code.length() - 3);
		 			strBuf.append("d.add('" + code + "','" + pcode + "','" + fm.getFunctionMenuName() + "','" + url.replace("$code", fm.getFunctionMenuCode()) + "', '', 'rightMenu');\n");
//					strBuf.append("d.add('" + fm.getFunctionMenuCode() + "',0,'" + fm.getFunctionMenuName() + "','" + url.replace("$code", fm.getFunctionMenuCode()) + "', '', 'rightMenu');\n");
//					strBuf.append(this.getSonFunctionMenuTree(request, response, fm.getFunctionMenuCode(), url));
				}
			}
			strBuf.append("document.write(d);\n");
	        return strBuf.toString();
	    }
	 
//	 public String  getSonFunctionMenuTree(HttpServletRequest request, HttpServletResponse response, String code, String url){
//		 FunctionMenuDAO dao = new FunctionMenuDAO(request, response);
//		 List list = dao.getSonCatalog(code);
//		 StringBuffer strBuf = new StringBuffer("");
//		 if(list != null && list.size() > 0){
//		 	for(int i = 0; i < list.size(); i ++){
//		 		FunctionMenu fm = (FunctionMenu)list.get(i);
//		 		strBuf.append("d.add('" + fm.getFunctionMenuCode() + "','" + code + "','" + fm.getFunctionMenuName() + "','" + url.replace("$code", fm.getFunctionMenuCode()) + "', '', 'rightMenu');\n");
//		 		strBuf.append(this.getSonFunctionMenuTree(request, response, fm.getFunctionMenuCode(), url));
//		 	}
//		 }
//		 return strBuf.toString();
//	 }
	 
	 
	 // 带有权限的地区树
	 public String getPowerAreaTree(HttpServletRequest request, HttpServletResponse response, String rootName, String rootUrl, String url, String value)
	    {	        
	        DataDictionaryDAO dao = new DataDictionaryDAO(request, response);
		 	StringBuffer strBuf = new StringBuffer("");
		 		strBuf.append("d = new dTree('d');\n");
		 		strBuf.append("d.add(0,-1,'" + rootName + "', '" + rootUrl + "', '', 'rightMenu');\n");
		 		if(value!=null && !"".equals(value)){
				 	value = value.replaceAll(",","','");
				 	List list = dao.getPowerCode("002", value);
				 	if(list != null && list.size() > 0){
				 		for(int i = 0; i < list.size(); i ++){
				 			DataDictionary data = (DataDictionary)list.get(i);
				 			String pcode = "0";
				 			String code = data.getDataDictionaryCode();
				 			if(i > 0)
					 			pcode = code.substring(0, code.length() - 3);
				 			strBuf.append("d.add('" + code + "','" + pcode + "','" + data.getDataDictionaryName() + "','" + url.replace("$code",data.getDataDictionaryCode()) + "', '', 'rightMenu');\n");
	//			 			strBuf.append(this.getSonDataDictionaryTree(request, response, data.getDataDictionaryCode(), url));
				 		}
				 	}
		 		}
			 	strBuf.append("document.write(d);\n");
	        return strBuf.toString();
	    }
	 
	 public String  getSonPowerAreaTree(HttpServletRequest request, HttpServletResponse response, String code, String url, String value){
		 DataDictionaryDAO dao = new DataDictionaryDAO(request, response);
		 List list = dao.getLoginRolePowerCode(code, value);
		 StringBuffer strBuf = new StringBuffer("");
		 if(list != null && list.size() > 0){
		 	for(int i = 0; i < list.size(); i ++){
		 		DataDictionary data = (DataDictionary)list.get(i);
		 		strBuf.append("d.add('" + data.getDataDictionaryCode() + "','" + code + "','" + data.getDataDictionaryName() + "','" + url.replace("$code",data.getDataDictionaryCode()) + "', '', 'rightMenu');\n");
	 			strBuf.append(this.getSonPowerAreaTree(request, response, data.getDataDictionaryCode(), url, value));
		 	}
		 }
		 return strBuf.toString();
	 }
	 
	 // 普通地区树
	 public String getCommonAreaTree(HttpServletRequest request, HttpServletResponse response, String rootName, String rootUrl, String url)
	    {	        
	        DataDictionaryDAO dao = new DataDictionaryDAO(request, response);
		 	StringBuffer strBuf = new StringBuffer("");
		 		strBuf.append("d = new dTree('d');\n");
		 		strBuf.append("d.add(0,-1,'" + rootName + "', '" + rootUrl + "', '', 'rightMenu');\n");
			 	List list = dao.getSonCatalog("002");
			 	if(list != null && list.size() > 0){
			 		for(int i = 0; i < list.size(); i ++){
			 			DataDictionary data = (DataDictionary)list.get(i);
			 			String pcode = "0";
			 			String code = data.getDataDictionaryCode();
			 			if(code.length() > 6)
			 				pcode = code.substring(0, code.length() - 3);
			 			strBuf.append("d.add('" + code + "','" + pcode + "','" + data.getDataDictionaryName() + "','" + url.replace("$code",data.getDataDictionaryCode()) + "', '', 'rightMenu');\n");
//			 			strBuf.append(this.getSonDataDictionaryTree(request, response, data.getDataDictionaryCode(), url));
			 		}
			 	}
			 	strBuf.append("document.write(d);\n");
	        return strBuf.toString();
	    }
	 
	 
	 // 带有权限的监察机构树
	 public String getPowerSuperviseOrganTree(HttpServletRequest request, HttpServletResponse response, String rootName, String rootUrl, String url, String value)
	    {
		 SuperviseOrganDAO dao = new SuperviseOrganDAO(request, response);
		 	StringBuffer strBuf = new StringBuffer("");
		 		strBuf.append("d = new dTree('d');\n");
		 		strBuf.append("d.add(0,-1,'" + rootName + "', '" + rootUrl + "', '', 'rightMenu');\n");
		 		if(value!=null && !"".equals(value)){
				 	value = value.replaceAll(",","','");
				 	List list = dao.getPowerCode(value);
				 	for(int i = 0; i < list.size(); i ++){
						SuperviseOrgan sup = (SuperviseOrgan)list.get(i);
						String pcode = "0";
			 			String code = sup.getSupOrganCode();
			 			if(i > 0)
			 				pcode = code.substring(0, code.length() - 3);
			 			strBuf.append("d.add('" + code + "','" + pcode + "','" + sup.getSupOrganName() + "','" + url.replace("$code",sup.getSupOrganCode()) + "', '', 'rightMenu');\n");
	//					strBuf.append("d.add('" + sup.getSupOrganCode() + "',0,'" + sup.getSupOrganName() + "','" + url.replace("$code",sup.getSupOrganCode()) + "', '', 'rightMenu');\n");
	//					strBuf.append(this.getSonSunperviseOrganTree(request, response, sup.getSupOrganCode(), url));
					}
		 		}
			 	strBuf.append("document.write(d);\n");
	        return strBuf.toString();
	     }
//	 public String  getSonSuperviseOrganTree(HttpServletRequest request, HttpServletResponse response, String code, String url, String value){
//		 DataDictionaryDAO dao = new DataDictionaryDAO(request, response);
//		 List list = dao.getLoginRolePowerCode(code, value);
//		 StringBuffer strBuf = new StringBuffer("");
//		 if(list != null && list.size() > 0){
//		 	for(int i = 0; i < list.size(); i ++){
//		 		DataDictionary data = (DataDictionary)list.get(i);
//		 		strBuf.append("d.add('" + data.getDataDictionaryCode() + "','" + code + "','" + data.getDataDictionaryName() + "','" + url.replace("$code",data.getDataDictionaryCode()) + "', '', 'rightMenu');\n");
//	 			strBuf.append(this.getSonPowerAreaTree(request, response, data.getDataDictionaryCode(), url, value));
//		 	}
//		 }
//		 return strBuf.toString();
//	 }
	 
}

